package com.caowj.composedemo.composable

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.caowj.composedemo.activity.*
import kotlinx.coroutines.launch


data class ItemInfo(val targetActivity: Class<out Activity>, val textStr: String)

/**
 * <pre>
 *     在Activity中展示新功能，未使用Navigation
 *     作者：Caowj
 *     邮箱：caowj1104@163.com
 *     日期：2022/3/2 15:15
 * </pre>
 */
@Composable
fun MainCompose2(context: Context) {
    val scaffoldState = rememberScaffoldState()
    val scope = rememberCoroutineScope()

    Scaffold(
        scaffoldState = scaffoldState,
        drawerContent = {
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                Text(text = "抽屉组件中的内容")
            }
        },
        //标题栏区域
        topBar = {
            TopAppBar(
                title = { Text("Scaffold 示例") },
                navigationIcon = {
                    IconButton(
                        onClick = {
                            scope.launch { scaffoldState.drawerState.open() }
                        }
                    ) {
                        Icon(Icons.Filled.Menu, contentDescription = "Localized description")
                    }
                }
            )
        },
        floatingActionButtonPosition = FabPosition.End,
        //悬浮按钮
        floatingActionButton = {
            ExtendedFloatingActionButton(
                text = { Text("Inc") },
                onClick = {
                    scope.launch {
                        scaffoldState.snackbarHostState.showSnackbar("点击了悬浮按钮")
                    }
                }
            )
        },
        //屏幕内容区域
        content = {
            _contentPage(context)
        }
    )
}

@Composable
private fun _contentPage(context: Context) {
    val itemList = mutableListOf<ItemInfo>()
    itemList.add(ItemInfo(PreviewActivity::class.java, "@Preview"))
    itemList.add(ItemInfo(TextActivity::class.java, "Text文本控件"))
    itemList.add(ItemInfo(ButtonActivity::class.java, "Button控件"))
    itemList.add(ItemInfo(MessageListActivity::class.java, "消息列表"))
    itemList.add(ItemInfo(MaterialDesignActivity::class.java, "MaterialDesign风格"))
    itemList.add(ItemInfo(ListActivity::class.java, "列表和点击"))
    itemList.add(ItemInfo(ModifierActivity::class.java, "Modifier修饰符"))
    itemList.add(ItemInfo(ConstrainLayoutActivity::class.java, "约束布局"))
    itemList.add(ItemInfo(ComplexPageActivity::class.java, "复杂页面"))
    itemList.add(ItemInfo(BottomNavigationActivity::class.java, "底部导航栏"))
    itemList.add(ItemInfo(CompositionLocalActivity::class.java, "数据分层"))


    LazyColumn(
        modifier = Modifier.padding(horizontal = 5.dp),
        //为每行添加间隔
        verticalArrangement = Arrangement.spacedBy(4.dp)
    ) {
        itemsIndexed(items = itemList) { index, item -> //遍历内容和索引
//            Text(text = "索引:$index -- 内容:$item")
            _IntentButton(context, item, index)
        }
    }
}

@Composable
private fun _IntentButton(mContext: Context, itemInfo: ItemInfo, index: Int) {
    Button(
        onClick = {
            mContext.startActivity(Intent(mContext, itemInfo.targetActivity))
        },
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(5.dp),
        contentPadding = PaddingValues(15.dp)
    ) {
        Text(text = "${index + 1}、${itemInfo.textStr}")
    }
}

